package com.xinyue.auto.reader;

import org.dom4j.Element;

import com.xinyue.auto.reader.process.IdProcess;
import com.xinyue.auto.reader.process.InterfaceProcess;
import com.xinyue.auto.reader.process.JoinProcess;
import com.xinyue.auto.reader.process.ManyInitProcess;
import com.xinyue.auto.reader.process.PropertyProcess;
import com.xinyue.auto.reader.process.Work;

/**
 * 单例工厂
 * 创建读取XML的processes的单例对象
 */
public class ManyFactory {
	//构造器
	private ManyFactory() {
	}
	
	//私有内部类
	private static class SingletonHolder{
        private static ManyFactory instance = new ManyFactory();
    }
	
	/**获得实例对象
	 * ProcessFactory单例
	 */
	public static ManyFactory getInstance() {
		return SingletonHolder.instance;
	}
	
	/**私有内部类
	 * 保证处理XML读取和验证对象始终是单例状态
	 */
	private static class SingletonProccess{
		private static Work work = new Work();
		//Xml数据
		private static InterfaceProcess inter = new InterfaceProcess(work);
		private static JoinProcess join = new JoinProcess(inter);
		private static PropertyProcess prop = new PropertyProcess(join);
		private static IdProcess id = new IdProcess(prop);
		//初始化参数，并做收尾
		private static ManyInitProcess init = new ManyInitProcess(id);
    }
	
	public ProcessContext initProccess(Element entity) throws Exception {
		Process.xml = entity;
		//注入值
		Inject.injectValue(SingletonProccess.init);
		Inject.injectValue(SingletonProccess.id);
		
		Inject.injectValue(SingletonProccess.prop);
		Inject.injectValue(SingletonProccess.join);
		Inject.injectValue(SingletonProccess.inter);
		Inject.injectValue(SingletonProccess.work);
		
		return SingletonProccess.init;
	}
}
